package com.swjd.jingle.fragment;

import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.swjd.jingle.R;
import com.swjd.jingle.adapter.MyTabAdapter;
import com.swjd.jingle.adapter.TypeItemAdapter;
import com.swjd.jingle.pojo.MyType;
import com.xuexiang.xui.widget.tabbar.VerticalTabLayout;
import com.xuexiang.xui.widget.tabbar.vertical.TabView;
import com.xuexiang.xui.widget.toast.XToast;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

public class TypeFragment extends BaseFragment implements View.OnClickListener {

    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.vt_stair)
    VerticalTabLayout vtStair;
    @BindView(R.id.gv)
    GridView gv;
    private MyTabAdapter myTabAdapter;
    private TypeItemAdapter typeItemAdapter;
    private List<MyType> type1s;
    private List<MyType.TypeListBean> typeList = new ArrayList<>();

    @Override
    public void initialize() {
        try {
            type1s = app.getTypes();
            typeList.addAll(type1s.get(0).getTypeList());
            myTabAdapter = new MyTabAdapter(activity, type1s);
            vtStair.setTabAdapter(myTabAdapter);
            typeItemAdapter = new TypeItemAdapter(activity, typeList);
            gv.setAdapter(typeItemAdapter);
        } catch (Exception e) {
            e.printStackTrace();
            XToast.normal(activity, "数据获取失败!", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void initViews() {
        gv.setOnItemClickListener((parent, view, position, id) -> {
            String input = typeList.get(position).getTName();
            SelectGoodsFragment selectGoodsFragment = new SelectGoodsFragment();
            selectGoodsFragment.setInput(input);
            fragmentManager.popBackStack();
            fragmentManager.beginTransaction().replace(R.id.layout_main, selectGoodsFragment)
                    .addToBackStack(null)
                    .commit();
        });
    }

    @Override
    public void initData() {
        tvTitle.setText("分类");
        vtStair.addOnTabSelectedListener(new VerticalTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabView tab, int position) {
                typeList.clear();
                typeList.addAll(type1s.get(position).getTypeList());
                typeItemAdapter.notifyDataSetChanged();
            }

            @Override
            public void onTabUnselected(TabView tab, int position) {
            }

            @Override
            public void onTabReselected(TabView tab, int position) {
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_type;
    }

    @Override
    @OnClick(R.id.img_back)
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_back:
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        fragmentManager.popBackStack();
                    }
                }, 500);
                break;
        }
    }
}
